package Hander.Updata;

import View.Add.AddProjectPersonnelView;
import View.LeaderView;
import View.Updata.UpdataProjectPersonnelView;
import entity.ProjectPersonnelTable;
import service.PersonnelService;
import service.PersonnelServiceImpl;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class UpdataProjectPersonnelViewHander implements ActionListener {
    private UpdataProjectPersonnelView updataProjectPersonnelView;
    private LeaderView leaderView;

    public UpdataProjectPersonnelViewHander(UpdataProjectPersonnelView updataProjectPersonnelView, LeaderView leaderView) {
        this.updataProjectPersonnelView = updataProjectPersonnelView;
        this.leaderView = leaderView;
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        JButton jButton = (JButton) e.getSource();
        String text = jButton.getText();
        if ("修改".equals(text)) {
            PersonnelService departmentService = new PersonnelServiceImpl();
            ProjectPersonnelTable projectPersonnelTable = updataProjectPersonnelView.buildDepartment();
            int key=Integer.parseInt(leaderView.getSelectedStudentIds()[0]);
            Boolean addResult = departmentService.updata(key,projectPersonnelTable.getAllAttributes(), updataProjectPersonnelView.getTableName());
            if (addResult) {
                leaderView.reloadTable(updataProjectPersonnelView.getTableName());
                JOptionPane.showMessageDialog(updataProjectPersonnelView, "修改成功");
                updataProjectPersonnelView.dispose();
            } else {
                System.out.println("修改错误！");
                JOptionPane.showMessageDialog(updataProjectPersonnelView, "修改失败");
            }
        }
    }
}
